﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RabbitMQCommon.WorkQueues
{
    public static class ProducerMessage
    {
        public static void SendMessage(string msg)
        {
            FactoryConnecation.QueueName = Queue.work.ToString();
            FactoryConnecation.Exchange = ExchangeName.workMode.ToString();
            FactoryConnecation.RoutKey = RoutingKey.workKey.ToString();
            using (var connection = FactoryConnecation.factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(FactoryConnecation.Exchange, ExchangeType.Direct, durable: false, autoDelete: false, arguments: null);

                //3.队列声明   参数:队列名 持久性:value(是持久/非持久) 是否独家的   自动删除  
                channel.QueueDeclare(queue: FactoryConnecation.QueueName,
                             durable: true,
                             exclusive: false,
                             autoDelete: false,
                             arguments: null);

                channel.QueueBind(FactoryConnecation.QueueName, FactoryConnecation.Exchange, FactoryConnecation.RoutKey, null);

                var message = GetMessage(msg);
                var body = Encoding.UTF8.GetBytes(message);

                var properties = channel.CreateBasicProperties();
                properties.Persistent = true;

                channel.BasicPublish(exchange: FactoryConnecation.Exchange,
                                     routingKey: FactoryConnecation.RoutKey,
                                     basicProperties: properties,
                                     body: body);
                Console.WriteLine(" [P] Send {0}", message);
            }
        }
        private static string GetMessage(string args)
        {
            return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
        }

    }
   
}

